package com.example.jiaju.ui;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.DecorateAdapter;
import com.example.jiaju.adapter.FurnitureRecommendAdapter;
import com.example.jiaju.adapter.HomeAdapter.WaterfallAdapter;
import com.example.jiaju.adapter.HomeAdapter.WaterfallHelperClass;
import com.example.jiaju.api.DecAndWorkApi;
import com.example.jiaju.api.FurnitureApi;
import com.example.jiaju.api.HouseApi;
import com.example.jiaju.databinding.FragmentHomeViewBinding;
import com.example.jiaju.entity.DecAndWork;
import com.example.jiaju.entity.Furniture;
import com.example.jiaju.entity.House;
import com.example.jiaju.ui.main.DecorationActivity;
import com.example.jiaju.ui.main.FurnitureActivity;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class HomeViewFragment extends Fragment {

    private String TAG="HomeViewFragment";
    RecyclerView waterfall;
    RefreshLayout refreshLayout;

    private Context context;
    //page页面
    int page=1;
    //选择标识
    int select;
    //楼盘
    RecyclerView.Adapter adapter;
    private List<House.PageDTO.ListDTO> waterfallLocations=new ArrayList<>();
    //装修
    private List<DecAndWork.PageDTO> decAndWorkList=new ArrayList<>();
    //家具
    private List<Furniture.PageDTO.ListDTO> furnitureList=new ArrayList<>();


    public HomeViewFragment(int select, Context context){
        this.select=select;
        switch(select){
//            case 0: Toast.makeText(context,"调用楼盘的接口",Toast.LENGTH_SHORT).show();break;
//            case 1:Toast.makeText(context,"调用装修的接口",Toast.LENGTH_SHORT).show();break;
//            default:Toast.makeText(context,"调用家具的接口",Toast.LENGTH_SHORT).show();break;
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_home_view, container, false);

        waterfall=view.findViewById(R.id.waterfull_recyclerview);
        waterfall.setHasFixedSize(true);
        waterfall.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        switch (select){

            //首页楼盘
            case 0:{
                HouseApi api= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(HouseApi.class);
                Call<House> call=api.getAllHouse(1,4);
                call.enqueue(new Callback<House>() {
                    @Override
                    public void onResponse(Call<House> call, Response<House> response) {
                        House house=response.body();
                        if(house==null){
                            Toast.makeText(getActivity(),"服务器请求数据异常",Toast.LENGTH_SHORT).show();
                        }else{
                            Log.d(TAG, "onResponse: "+house);
                            waterfallLocations=house.page.list;

                            //实现瀑布流
                            adapter=new WaterfallAdapter(select,waterfallLocations,getContext());
                            waterfall.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                    }
                    @Override
                    public void onFailure(Call<House> call, Throwable t) {

                    }
                });
            };break;
            //首页装修
            case 1:{
                DecAndWorkApi api=RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(DecAndWorkApi.class);
                Call<DecAndWork> call=api.getAllDecAndWork(1,4);
                call.enqueue(new Callback<DecAndWork>() {
                    @Override
                    public void onResponse(Call<DecAndWork> call, Response<DecAndWork> response) {
                        DecAndWork decAndWork=response.body();
                        if(decAndWork==null){
                            Toast.makeText(getActivity(),"服务器请求数据异常",Toast.LENGTH_SHORT).show();
                        }else{
                            decAndWorkList=decAndWork.page;
                            adapter=new WaterfallAdapter(1,decAndWorkList,getContext());
                            waterfall.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(Call<DecAndWork> call, Throwable t) {

                    }
                });
            };break;
            //首页家具
            case 2:{
                //1.获取到API接口
                FurnitureApi furnitureApi= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(FurnitureApi.class);
                //2.获取到实体类
                Call<Furniture> call=furnitureApi.getAllFurniture(1,4);
                //3.消息请求队列，取出数据
                call.enqueue(new Callback<Furniture>() {
                    @Override
                    public void onResponse(Call<Furniture> call, Response<Furniture> response) {
                        Furniture furniture=response.body();
                        if(furniture==null){
                            Toast.makeText(getContext(),"家具接口数据请求异常！",Toast.LENGTH_SHORT).show();
                        }else{
                            //家具为你推荐
                            furnitureList=furniture.page.list;
                            adapter=new WaterfallAdapter(2,furnitureList, getContext());
                            waterfall.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                    }
                    @Override
                    public void onFailure(Call<Furniture> call, Throwable t) {

                    }
                });
            };break;
        }

        //实现下拉加载更多
        refreshLayout=view.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshFooter(new BallPulseFooter(getParentFragment().getActivity()).setSpinnerStyle(SpinnerStyle.Scale),0,0);
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            HouseApi api= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(HouseApi.class);
            Call<House> call=api.getAllHouse(page++,4);
            call.enqueue(new Callback<House>() {
                @Override
                public void onResponse(Call<House> call, Response<House> response) {
                    House house=response.body();
                    if(house==null){
                        Toast.makeText(getActivity(),"服务器请求数据异常",Toast.LENGTH_SHORT).show();
                    }else{
                        Log.d(TAG, "onResponse: "+house);
                        //添加新的数据
                        waterfallLocations.addAll(house.page.list);

                        adapter.notifyDataSetChanged();
                    }
                }
                @Override
                public void onFailure(Call<House> call, Throwable t) {

                }
            });
            refreshLayout.finishLoadMore(2000/*,false*/);
        });

        return view;
    }

    //自定义属性绑定ImageView
    @BindingAdapter("imageUrl")
    public static void bindImageUrl(ImageView view, String url){
        Glide.with(view)
                .load(url)
                .into(view);
    }
}